package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 用户名效验
 */
public class UserExistCheckServlet extends HttpServlet {

    private UserService userservice = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        PrintWriter out = response.getWriter();
        int count = 1;
        if (userservice.existx(username)) {
            System.out.println("=====>用户名"+username+"已被注册");
        } else {
            count = 0;
            System.out.println("=====>用户名"+username+"可以注册");
        }
        String result = "{\"code\":"+ count +"}";
        out.write(result);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
